﻿using B.MES.Customer.Domain;
using B.MES.Customer.Domain.DTO;
using B.MES.Customer.Infrastructure.IRepository;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yitter.IdGenerator;

namespace B.MES.Customer.Infrastructure.Repository
{
    /// <summary>
    /// 生产仓储实现层
    /// </summary>
    public class ProduceRepository : BaseRepository<Produce>, IProduceRepository
    {
        private readonly ISqlSugarClient _db;
        public ProduceRepository(ISqlSugarClient db) : base(db)
        {
            _db = db;
        }

        /// <summary>
        /// 新增生产
        /// </summary>
        /// <param name="produce">需要添加的生产数据</param>
        /// <returns>受影响行数</returns>
        public int AddProduce(Produce produce,string? SalesOrderNumber)
        {

            produce.ProCode = YitIdHelper.NextId().ToString();
            produce.ProStatus = 0;
            _db.Updateable<SalesManagement>().SetColumns(x => x.IsDelete == false).Where(x => x.SalesOrderNumber == SalesOrderNumber).ExecuteCommand();
            return _db.Insertable(produce).ExecuteCommand();
        }

        /// <summary>
        /// 生产的显示分页查询
        /// </summary>
        /// <param name="ProCode">生产单号</param>
        /// <param name="pageIndex">当前是第几页</param>
        /// <param name="pageSize">每页需要显示多少条数据</param>
        /// <returns>生产的查询显示分页</returns>
        public PageApiResut<Produce> GetProduceShow(string? ProCode,int pageIndex,int pageSize)
        {
            var ProduceList = _db.Queryable<Produce>().WhereIF(!string.IsNullOrEmpty(ProCode), x => x.ProCode.Contains(ProCode));
            var totalCount = ProduceList.Count();
            var pageList = ProduceList.ToPageList(pageIndex, pageSize).ToList();
            var list = new PageApiResut<Produce>()
            {
                TotalCount = totalCount,
                Data = pageList
            };
            return list;
        }


        /// <summary>
        /// 获取生产信息
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public List<Produce> GetProduct()
        {
            var result=_db.Queryable<Produce>().ToList();
            return result;
        }


        /// <summary>
        /// 删除生产信息
        /// </summary>
        /// <param name="ProId">ID</param>
        /// <returns>受影响行数</returns>
        public int DelteProduce(int ProId)
        {
            var result = _db.Deleteable<Produce>().Where(x => x.ProId == ProId).ExecuteCommand();
            return result;
        }

        /// <summary>
        /// 根据生产Id获取生产信息
        /// </summary>
        /// <param name="ProCode">生产Id</param>
        /// <returns>生产信息</returns>
        public Produce GetProduceByProId(int ProId)
        {
            var result = _db.Queryable<Produce>().InSingle(ProId);
            return result;
        }

        /// <summary>
        /// 更新生产信息
        /// </summary>
        /// <param name="produce">生产信息</param>
        /// <returns>受影响行数</returns>
        public int UpdateProduce(Produce produce)
        {
            produce.ProStatus = 0;
            var result = _db.Updateable(produce).ExecuteCommand();
            return result;    
        }
    }
}
